﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.order
{
    public partial class ProductAdd : System.Web.UI.Page
    {
        readonly Car theCar = new Car();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindProductList();
            }
        }

        protected decimal RemanantsAmount
        {
            get
            {
                if (ViewState["remamantsAmount"] == null)
                {
                    return decimal.Parse(Request.QueryString["remanants"]);
                }
                else
                {
                    return (decimal)ViewState["remamantsAmount"];
                }
            }
            set
            {
                ViewState["remamantsAmount"] = value;
            }
        }
        private string OrderCode
        {
            get
            {
                return Request.QueryString["orderCode"];
            }
        }

        private int ProfessionTypeId
        {
            get
            {
                return int.Parse(Request.QueryString["typeId"]) ;
            }
        }

        void BindProductList()
        {
            DataTable table = SQLAccess.GreatDs("select proid,proname,proprice1,proprice3,propic2  from DB_Shoppro").Tables[0];
           // DataTable table = SQLAccess.GreatDs("select proid,proname,proprice1,proprice3,propic2  from DB_Shoppro where ProfessionAreaTypeId= " + Request.QueryString["typeId"]).Tables[0];
            this.GridView1.DataSource = table.DefaultView;
            this.GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            decimal dRemanants = 0;
            if (e.CommandName == "add")
            {
                //dSpendMoney = theCar.GetSpendMoney(OrderCode, ProfessionTypeId);
               // dRemanants = RemanantsAmount - dSpendMoney;
                Label lblAmount = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("lblAmount") as Label;
                TextBox txtNum = GridView1.Rows[int.Parse(e.CommandArgument.ToString())].FindControl("txtNum") as TextBox;
                dRemanants = RemanantsAmount - decimal.Parse(lblAmount.Text);

                if (dRemanants >= 0)
                {
                    //添加数据
                    tbCar oCar = new tbCar();
                    oCar.OrderCode = OrderCode;
                    oCar.ProdNumber = int.Parse(txtNum.Text);
                    oCar.ProdPrice = decimal.Parse(lblAmount.Text);
                    oCar.ProductId = int.Parse(((HyperLink)GridView1.Rows[int.Parse(e.CommandArgument.ToString())].Cells[0].Controls[0]).Text);
                    oCar.UpdateDate = DateTime.Now;
                    theCar.Add(oCar);
                    RemanantsAmount = dRemanants;
                    base.Response.Write("<script  language='javascript'>alert('添加成功！');window.opener.location.reload(); </script>");
                }
                else
                {
                    base.Response.Write("<script  language='javascript'>alert('余额不够，请选择其它商品！');</script>");
                }


            }
        }
    }
}